//
//  BHSubscriptionAnimationItemView.swift
//  APSwift
//
//  Created by jianbo.pei on 2025/3/21.
//

import UIKit
import Then
import SnapKit

class BHSubscriptionAnimationItemView: UIView {
    typealias ItemTuple = (bgColor: UIColor, textFont: UIFont, textColor: UIColor)
    
    enum ItemIndex: Int, CaseIterable {
        case first = 0
        case second
        case third
        case fourth
        case fifth
        case sixth
    }
    public private(set) var titleLabel: UILabel!
    
    public private(set) var itemIdx: ItemIndex
    
    required init(itemIdx: ItemIndex) {
        self.itemIdx = itemIdx
        super.init(frame: .zero)
        makeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func makeUI() {
        layer.masksToBounds = true
        layer.cornerRadius = 15
        
        titleLabel = UILabel().then({ make in
            addSubview(make)
            make.numberOfLines = 0
            make.snp.makeConstraints { make in
                make.leading.equalTo(15)
                make.trailing.equalTo(-15)
                make.center.equalToSuperview()
            }
        })
    }
    
    
    public func resetItemIndex(_ idx: ItemIndex) {
        self.itemIdx = idx
    }
}

extension BHSubscriptionAnimationItemView.ItemIndex {    
    var tuple: BHSubscriptionAnimationItemView.ItemTuple {
        switch self {
        case .first, .second, .fourth, .fifth, .sixth:
            return (
                UIColor(hexString: "173430", alpha: 0.06),
                UIFont.systemFont(ofSize: 14),
                UIColor(hexString: "173430"))
        case .third:
            return (
                UIColor(hexString: "FFDC88"),
                UIFont.boldSystemFont(ofSize: 14),
                UIColor(hexString: "173430"))
        }
    }
    
    var bottomSpacing: CGFloat {
        switch self {
        case .first:
            return 8
        case .second:
            return 12
        case .third:
            return 12
        case .fourth:
            return 8
        case .fifth:
            return 8
        case .sixth:
            return 0
        }
    }
    
    var nextY: CGFloat {
        switch self {
        case .first:
            return -itemH - 8
        case .second:
            return topItemY
        case .third:
            return Self.second.nextY + itemH + Self.first.bottomSpacing
        case .fourth:
            return Self.third.nextY + itemH + Self.third.bottomSpacing
        case .fifth:
            return Self.fourth.nextY + itemH + Self.fourth.bottomSpacing
        case .sixth:
            return Self.fifth.nextY + itemH + Self.fifth.bottomSpacing
        }
    }
    
    var nextX: CGFloat {
        if self == .fourth {
            return specialItemX
        } else {
            return normalItemX
        }
    }
    
    var nextIndex: BHSubscriptionAnimationItemView.ItemIndex {
        switch self {
        case .first:
            return .sixth
        case .second:
            return .first
        case .third:
            return .second
        case .fourth:
            return .third
        case .fifth:
            return .fourth
        case .sixth:
            return .fifth
        }
    }
    
    var normalY: CGFloat {
        switch self {
        case .first:
            return topItemY
        case .second:
            return Self.first.normalY + itemH + Self.first.bottomSpacing
        case .third:
            return Self.second.normalY + itemH + Self.second.bottomSpacing
        case .fourth:
            return Self.third.normalY + itemH + Self.third.bottomSpacing
        case .fifth:
            return Self.fourth.normalY + itemH + Self.fourth.bottomSpacing
        case .sixth:
            return Self.fifth.normalY + itemH + Self.fifth.bottomSpacing
        }
    }
    
    var normalX: CGFloat {
        if self == .third {
            return specialItemX
        } else {
            return normalItemX
        }
    }
}
